const axios = require("axios").default;
const fs = require('fs');
const path = require('path')
const cheerio = require('cheerio');

function getLinksArray(target) {
    const $ = cheerio.load(target);
    const temp = $('tbody .item .nbg');
    const result = temp.map((index,item)=>{
        return $(item).attr('href')
    })
    const returnArr = Array.prototype.slice.call(result)
    return returnArr
}

async function fetchBookInfo(url){
 
    const res = await axios.get(url);
    const $ = cheerio.load(res.data);
    const bookName = $('#wrapper h1 span').text()
    const bookImg = $('#mainpic .nbg img').attr('src');
    const temp = $('#info>span').get()
    let publishDate
    temp.forEach((item,i)=>{
       if($(item).text()==='出版年:'){
            publishDate = $(item.nextSibling).text()
       }
    })
    const temp2 = $('#info>span .pl').siblings()
    // 拼接作者
    const author = $(temp2).text()
    // console.log(temp[2].nextSibling.nodeValue)
    // console.log($('#info>.pl').get())
    const bookObj = {bookName,bookImg,publishDate,author}
    return bookObj;
}

async function main(){
    // 拿到榜单网页代码
    const pageInfo = await axios.get('https://book.douban.com/top250?icn=index-book250-all');
    const originTarget = pageInfo.data;
    //得到每本书的链接
    const bookList =getLinksArray(originTarget);
    //遍历每个链接 拿到书籍信息
    // console.log(bookList)
    let resList = bookList.map((item)=>{
        return fetchBookInfo(item)
    })
    return Promise.all(resList)
}
// main();


// 测试将以下书籍加入库

const book = require("../models/book");
async function addbook(){
    const target = await main();
    console.log(target)
    await book.bulkCreate(target);
    console.log("ok!")
}
addbook()
// {
//     bookName: '红楼梦',
//     bookImg: 'https://img1.doubanio.com/view/subject/s/public/s1070959.jpg',
//     publishDate: ' 1996-12',
//     author: '[清] 曹雪芹 著高鹗'
//   }